package org.smr.ministore.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.blade.msg.Message;
import org.blade.utils.Pager;
import org.smr.ministore.entities.Authority;
import org.smr.ministore.service.AuthorityService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller("AuthorityController")
public class AuthorityController {

	@Resource
	private AuthorityService service;
	
	/**
	 * 保存编辑后的Authority信息
	 * @param Authority
	 * @return
	 */
	@ResponseBody
	@RequestMapping(method = RequestMethod.PATCH, value = "/Authority/edit")
	public Message modify(@RequestBody Authority Authority){
		Message msg = new Message();
		
		if(null == Authority.getId()){
			msg.setStatus(service.save(Authority));
		}else{
			msg.setStatus(service.updateById(Authority));
		}
		return msg;
	}
	
	/**
	* 跳转到列表查询Authority
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/Authority/list-page")
	public ModelAndView toList(HttpServletResponse response, HttpServletRequest request, Long parentId, Integer type ){

		ModelAndView mov = new ModelAndView("/Authority/Authority-list");
		List<Authority> list = service.getByParentAndType(parentId, type);
		mov.addObject("list", list);
		return mov;
	}

	/**
	*  根据id查询Authority
	* @return
	*/
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "/Authority/{id}")
	public Authority get( @PathVariable Long id ){
		return service.queryById(id);
	}


    /**
	 * 分页查询Authority信息
	 * @param Authority
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/Authority/list")
	public Pager<Authority> list( Authority Authority , long pageSize, long pageNum ){

		return service.paginate(Authority, pageSize, pageNum);

	}
	
	
	/**
	 * 根据ID删除Authority信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/Authority/remove",method = RequestMethod.DELETE)
	public Message remove( Long id){
		
		Message msg = new Message();
		msg.setStatus(service.deleteById(id));
		return msg;
	}

}
